// function qs(ele) {
//     return document.querySelector(ele)
// }

// 接口地址：https://www.apifox.cn/apidoc/shared-36f0cbc0-9064-44bc-850a-805e263bf494/api-25827426
// 简化箭头函数


axios.defaults.baseURL = 'http://124.223.14.236:3001/api'

//初始化为安徽，合肥，瑶海
async function init() {

    const res = await axios.get('/city/province')
    // console.log(res);

    // 渲染省份
    const provinceStr = res.data.data.map
        (item => `<option value="${item.province}">${item.name}</option>`).join('')
    $('#province').append(provinceStr)
    // 默认安徽
    $('#province').val(34)

    // 获取安徽的市
    const resCity = await axios.get('/city/city', {
        params: {
            provinceCode: '34'
        }
    })
    // console.log(resCity);

    // 渲染城市
    const cityStr = resCity.data.data.map
        (item => `<option value="${item.city}">${item.name}</option>`).join('')
    $('#city').append(cityStr)
    $('#city').val('01')

    // 获得合肥的区县
    const resArea = await axios.get('/city/area', {
        params: {
            provinceCode: '34',
            cityCode: '01'
        }
    })
    // console.log(resArea);
    // 渲染区县
    const areaStr = resArea.data.data.map
        (item => `<option value="${item.area}">${item.name}</option>`).join('')
    $('#county').append(areaStr)
    $('#county').val('02')
}
init()

// 省份change事件
$('#province').on('change', async function () {
    const resCity = await axios.get('/city/city', {
        params: {
            provinceCode: $(this).val()
        }
    })
    console.log(resCity);
    const cityStr = resCity.data.data.map
        (item => `<option value="${item.city}">${item.name}</option>`).join('')
    $('#city').empty().append(`<option value="">请选择城市</option>` + cityStr)

    $('#county').empty().append(`<option value="">请选择区县</option>`)
})

// 城市change事件
$('#city').on('change',async function(){
    const resArea = await axios.get('/city/area', {
        params: {
            provinceCode: $('#province').val(),
            cityCode:$(this).val()
        }
    })
    console.log(resArea);
    const areaStr = resArea.data.data.map
        (item => `<option value="${item.city}">${item.name}</option>`).join('')
    $('#county').empty().append(`<option value="">请选择区县</option>` + areaStr)

})